package com.htc.callcare.adapter;

import java.util.ArrayList;
import java.util.Hashtable;

import com.htc.callcare.R;
import com.htc.callcare.activities.VoiceMailActivity;
import com.htc.callcare.adapter.RecordAdapter.ViewHolder;
import com.htc.callcare.database.Database;
import com.htc.callcare.model.VoiceMailModel;

import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class VoiceMailAdapter extends BaseAdapter{
	VoiceMailActivity activity;
	ArrayList<VoiceMailModel> list;
	Hashtable hashtable;
	Database database;
	String contact_name;
	LayoutInflater inflater;
	

	
	public ArrayList<VoiceMailModel> getList() {
		return list;
	}

	public void setList(ArrayList<VoiceMailModel> list) {
		this.list = list;
	}

	public VoiceMailAdapter(VoiceMailActivity activity,
			ArrayList<VoiceMailModel> list, Hashtable hashtable) {
		super();
		this.activity = activity;
		this.list = list;
		this.hashtable = hashtable;
		inflater = LayoutInflater.from(activity);
		database = new Database(activity);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final VoiceMailModel model = list.get(position);
		ViewHolder holder;
		convertView = null;
		holder = new ViewHolder();
		if (model.getSeparator() == 0) {
			// inflate row file
			convertView = inflater.inflate(R.layout.voice_mail_row, null);
			convertView.setTag(holder);
			holder.voice_mail_title = (TextView) convertView
					.findViewById(R.id.voice_mail_title);
			holder.voice_mail_time = (TextView) convertView
					.findViewById(R.id.voice_mail_time);
			holder.voice_mail_checkbox = (CheckBox) convertView
					.findViewById(R.id.voice_mail_check_box);
			//BOLD if it is NEW
			if (database.findVoiceMailNew(model.getFileUri()) == 1) {
				holder.voice_mail_title.setTypeface(null, Typeface.BOLD);
			}

			if (hashtable.containsKey(model.getCallNumber())) {
				contact_name = (String) hashtable.get(model.getCallNumber())
						+ " (" + model.getCallNumber() + ")";

			} else {
				contact_name = model.getCallNumber();
			}

			holder.voice_mail_title.setText(contact_name);
			holder.voice_mail_time.setText(model.getCallTime());
			//Init check BOX
			if(database.findVoiceMailCheck(model.getFileUri())){
				holder.voice_mail_checkbox.setChecked(true);
			}else{
				holder.voice_mail_checkbox.setChecked(false);
			}
			//Check BOX listener
			holder.voice_mail_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(isChecked){
						database.addVoiceMailCheck(model.getFileUri());
					}else{
						database.deleteCheckRecord(model.getFileUri());
					}
				}
			});

			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					database.updateVoiceMailNewIsRead(model.getFileUri());
					activity.showPlayerView();
					activity.setVoiceMailFile(model.getFileUri());

				}
			});
			
			convertView.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					activity.setSelectedVoiceMail(model, contact_name);
					return false;
				}
			});
		} else {

			convertView = inflater.inflate(R.layout.separator, null);
			convertView.setTag(holder);
			holder.voice_mail_title = (TextView) convertView
					.findViewById(R.id.textSeparator);
			holder.voice_mail_title.setText(model.getCallDay());
		}
		return convertView;
	}
	
	class ViewHolder {
		TextView voice_mail_title;
		TextView voice_mail_time;
		CheckBox voice_mail_checkbox;

	}
}
